/**
 * Created by zouy on 17-3-14.
 */
/**
 * Created by zouyao on 2017/3/6.
 */
var m = angular.module("mainapp",[]);
var constant = m.constant('constRef',[["我的课程","评价教师","评分教师","个人信息","评估记录","自我评估","查看教师评估记录","评估管理","用户管理"],//leftOperation
                            ["评估记录","自我评估","查看教师评估记录","用户管理"]]);

var topCtrl = m.controller("topcontroller",function ($rootScope,$scope,constRef) {

    $scope.getLoger = function(){
        $.ajax({
            type:"POST",
            url:"/login/getLoger",
            data:{"id":$rootScope.idOfLoger},
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.$apply(function(){
                    $scope.editLogerArray = new Array();
                    var obj = {};
                    obj['id'] = data.value[0].id;
                    obj['username'] = data.value[0].username;
                    obj['email'] = data.value[0].email;
                    obj['tel'] = data.value[0].tel;
                    $scope.editLogerArray.push(obj);
                });
            }
        });
    };

    $scope.modifyPassWord = function () {
        $scope.getLoger();
        $rootScope.editPassWord = "";
        $rootScope.editPassWordNew = "";
        $("#modalid-modifyPWD").modal("toggle");
    };

    $rootScope.modifyPWD = function(){
        $.ajax({
            type:"POST",
            url:"/login/modifyPWD",
            data:{"id":$rootScope.idOfLoger,"passWord":$scope.editPassWord,
                "newPassWord":$scope.editPassWordNew},
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
                console.log(data);
                $scope.$apply(function(){
                    if(data.message == "旧密码不正确"){
                        alert("旧密码不正确");
                        /*$rootScope.justForModalInfomation = "旧密码不正确!";
                        $("#modalid-toastInfo").modal("toggle");*/
                    }else {
                        $rootScope.justForModalInfomation = "修改密码成功!请重新登录!";
                        $("#modalid-mpswoffconf").modal("toggle");
                    }
                });
            }
        });
    };

    $rootScope.offFunction = function () {
        window.location.href = "/login.html";
    };

    $rootScope.offFunctionOk = function () {
        $("#modalid-offconf").modal("toggle");
    };

});

var indexCtrl = m.controller("indexcontroller",function ($rootScope,$scope,constRef) {

    $scope.constRef = constRef;
    $rootScope.justForModalInfomation = "";
    $rootScope.nav = -1;

    $scope.GetQueryString = function(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null){return  decodeURI(r[2]);} return null;
    };
    $scope.htmlInit = function(){
        $rootScope.usernameOfLoger = $scope.GetQueryString("userName");
        $rootScope.idOfLoger = $scope.GetQueryString("userId");
        $rootScope.roleOfLoger = $scope.GetQueryString("role");
        if(parseInt($rootScope.idOfLoger) < 1 || $rootScope.idOfLoger == null){
            //window.location.href = "/login.html";
        }
        /*console.log($rootScope.usernameOfLoger);
        console.log($rootScope.idOfLoger);*/
    };
    $scope.htmlInit();//init the userId(from loger)

    $scope.rightDiv = function(obj){
        if(obj == "我的课程"){
            $rootScope.nav = 0;
            $scope.activeStatus = 0;

            $rootScope.mc_init();
        }/*else if(obj == "评价教师"){
            $rootScope.nav = 1;
            $scope.activeStatus = 1;

            $rootScope.mcr_init();
        }else if(obj == "评分教师"){
            $rootScope.nav = 2;
            $scope.activeStatus = 2;

        }*/else if(obj == "个人信息"){
            $rootScope.nav = 3;
            $scope.activeStatus = 3;

            $rootScope.mi_init();
        }else if(obj == "自我评估"){
            $rootScope.nav = 4;
            $scope.activeStatus = 4;

            $rootScope.tes_init();
        }else if(obj == "评估记录"){
            $rootScope.nav = 5;
            $scope.activeStatus = 5;

            $rootScope.tr_init();
        }else if(obj == "查看教师评估记录"){
            $rootScope.nav = 6;
            $scope.activeStatus = 6;

            $rootScope.mts_init();
        }else if(obj == "用户管理"){
            $rootScope.nav = 7;
            $scope.activeStatus = 7;

            $rootScope.um_init();
        }
    };

});
